# Makefile for gnustandards conversion using reposurgeon

CVS_HOST = cvs.savannah.gnu.org
CVS_MODULE = gnustandards
REMOTE_URL = cvs://$(CVS_HOST)/sources/gnustandards\#$(CVS_MODULE)
VERBOSITY = "verbose 1"
REPOSURGEON = reposurgeon

# Configuration ends here.

.PHONY: local-clobber remote-clobber gitg gc compare clean dist stubmap
# Tell make not to auto-remove tag directories, because it only tries rm
# and hence fails.
.PRECIOUS: gnustandards-%-checkout gnustandards-%-git

default: gnustandards-git

# Build the converted repo from the second-stage fast-import stream.
gnustandards-git: gnustandards.fi
	rm -fr $@; $(REPOSURGEON) "read <$<" "prefer git" \
	  "rebuild gnustandards-git"

CONVERSION_OBJS = 	\
  gnustandards.cvs	\
  gnustandards.opts	\
  gnustandards.lift	\
  gnustandards.map	\

# Build the second-stage fast-import stream from the first-stage stream dump
gnustandards.fi: $(CONVERSION_OBJS)
	$(REPOSURGEON) $(VERBOSITY) "script gnustandards.opts"		\
	  "read $(READ_OPTIONS) <gnustandards.cvs" 			\
	  "authors read <gnustandards.map" "sourcetype cvs"		\
	  "prefer git" "script gnustandards.lift"			\
	  "legacy write >gnustandards.fo" "write >gnustandards.fi"

# Build the first-stage stream dump from the local mirror.
gnustandards.cvs: gnustandards-mirror
	repotool mirror $<
	(cd gnustandards-mirror/ >/dev/null; repotool export) > $@

# Build a local mirror of the remote repository.
gnustandards-mirror:
	repotool mirror $(REMOTE_URL) $@

# Make a local checkout of the source mirror for inspection.
gnustandards-checkout: gnustandards-mirror
	cd $< >/dev/null; repotool checkout ../$@

# Make a local checkout of the source mirror for inspection at a specific tag.
gnustandards-%-checkout: gnustandards-mirror
	cd $< >/dev/null; repotool checkout ../gnustandards-$*-checkout $*

# Force rebuild of first-stage stream from the local mirror on the next make.
local-clobber: clean
	rm -fr gnustandards.fi gnustandards-git *~ .rs* \
	  gnustandards-conversion.tar.gz gnustandards-*-git

# Force full rebuild from the remote repo on the next make.
remote-clobber: local-clobber
	rm -fr gnustandards.cvs gnustandards-mirror \
	  gnustandards-checkout gnustandards-*-checkout

# Get the (empty) state of the author mapping from the first-stage stream
stubmap: gnustandards.cvs
	$(REPOSURGEON) "read <$<" "authors write >gnustandards.map"

# Compare the histories of the unconverted and converted repositories at head
# and all tags.
EXCLUDE = -x CVS -x .cvs -x .git -x .cvsignore -x .gitignore
headcompare:
	repotool compare $(EXCLUDE) gnustandards-checkout gnustandards-git
tagscompare:
	repotool compare-tags $(EXCLUDE) gnustandards-checkout gnustandards-git

# General cleanup and utility
clean:
	rm -fr *~ *.cvs *.fi *.fo *-git gnustandards-conversion.tar.gz .rs*

# Bundle up the conversion metadata for shipping
SOURCES = Makefile gnustandards.lift gnustandards.map $(EXTRAS)
gnustandards-conversion.tar.gz: $(SOURCES)
	tar -h --transform 's:^:gnustandards-conversion/:' -czvf $@ $*

dist: gnustandards-conversion.tar.gz

# Browse the generated git repository
gitg: gnustandards-git
	cd gnustandards-git; $@ --all

# Run a garbage-collect on the generated git repository.  Import doesn't.
# This repack call is the active part of gc --aggressive.  This call is tuned
# for very large repositories.
gc: gnustandards-git
	cd gnustandards-git
	time git -c pack.threads=1 repack -AdF --window=1250 --depth=250
